import time
from Sentry3 import *
from mpython import *
from machine import UART


uart1 = UART(1, baudrate=57600, tx=Pin.P14, rx=Pin.P13)

Sentry3 = Sentry3(0x60)

# 自定义函数
def UART():
  global my_variable, i, num
  Sentry3.begin(uart1)
def I2C():
  global my_variable, i, num
  Sentry3.begin(i2c)
def Result_Display():
  global my_variable, i, num
  # Sentry3不主动返回检测识别结果，需要主控板发送指令进行读取。读取的流程：首先读取识别结果的数量，接收到指令后，Sentry3会刷新结果数据，如果结果数量不为零，那么主控再发送指令读取结果的相关信息。请务必按此流程构建程序。
  if ((Sentry3.GetValue(sentry3_vision_e.kVisionQrCode, sentry_obj_info_e.kStatus)) > 0):
    oled.DispChar((Sentry3.GetString(sentry3_vision_e.kVisionQrCode)), 0, (2-1)*16, 1)
    # Sentry3最多识别1个二维码，故只需获取并处理结果1的相关数据
    oled.DispChar((str("x=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionQrCode,sentry_obj_info_e.kXValue,1)))), 0, 32, 1)
    oled.DispChar((str("y=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionQrCode,sentry_obj_info_e.kYValue,1)))), 64, 32, 1)
    oled.DispChar((str("w=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionQrCode,sentry_obj_info_e.kWidthValue,1)))), 0, 48, 1)
    oled.DispChar((str("h=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionQrCode,sentry_obj_info_e.kHeightValue,1)))), 64, 48, 1)
    oled.show()
    time.sleep(0.2)
    oled.fill_rect(0, (2-1)*16, 128, 16, 0)
    oled.fill_rect(0, (3-1)*16, 128, 16, 0)
    oled.fill_rect(0, (4-1)*16, 128, 16, 0)
    oled.show()


# 等待Sentry3初始化完毕，此延时不可省略
time.sleep(2)
UART()
# Sentry3同一时间只能识别一个由不超过10个字符所生成的二维码
Sentry3.VisionBegin(sentry3_vision_e.kVisionQrCode)
oled.DispChar("Algo:QrCode", 0, (1-1)*16, 1)
oled.show()
while True:
  Result_Display()
